﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheVerge.Base.Util;

namespace TheVerge.Base.Models
{
    public class VMBrands : VMSection<VMBrand>
    {
        public override string DisplayName { get { return @"Brands"; } }

        public IEnumerable<Group<VMBrand>> BrandGroups
        {
            get
            {
                if (All == null) return null;

                var groups = (from b in All
                              group b by GetKey(b) into grp
                              orderby grp.Key
                              select new Group<VMBrand>(grp.Key, grp));


                return from b in groups.Union(emptyGroups)
                       orderby b.Title
                       select b;
            }
        }

        private static List<Group<VMBrand>> emptyGroups = new List<Group<VMBrand>>() {
            new Group<VMBrand>(@"#", new List<VMBrand>()),
            new Group<VMBrand>(@"a", new List<VMBrand>()),
            new Group<VMBrand>(@"b", new List<VMBrand>()),
            new Group<VMBrand>(@"c", new List<VMBrand>()),
            new Group<VMBrand>(@"d", new List<VMBrand>()),
            new Group<VMBrand>(@"e", new List<VMBrand>()),
            new Group<VMBrand>(@"f", new List<VMBrand>()),
            new Group<VMBrand>(@"g", new List<VMBrand>()),
            new Group<VMBrand>(@"h", new List<VMBrand>()),
            new Group<VMBrand>(@"i", new List<VMBrand>()),
            new Group<VMBrand>(@"j", new List<VMBrand>()),
            new Group<VMBrand>(@"k", new List<VMBrand>()),
            new Group<VMBrand>(@"l", new List<VMBrand>()),
            new Group<VMBrand>(@"m", new List<VMBrand>()),
            new Group<VMBrand>(@"n", new List<VMBrand>()),
            new Group<VMBrand>(@"o", new List<VMBrand>()),
            new Group<VMBrand>(@"p", new List<VMBrand>()),
            new Group<VMBrand>(@"q", new List<VMBrand>()),
            new Group<VMBrand>(@"r", new List<VMBrand>()),
            new Group<VMBrand>(@"s", new List<VMBrand>()),
            new Group<VMBrand>(@"t", new List<VMBrand>()),
            new Group<VMBrand>(@"u", new List<VMBrand>()),
            new Group<VMBrand>(@"v", new List<VMBrand>()),
            new Group<VMBrand>(@"w", new List<VMBrand>()),
            new Group<VMBrand>(@"x", new List<VMBrand>()),
            new Group<VMBrand>(@"y", new List<VMBrand>()),
            new Group<VMBrand>(@"z", new List<VMBrand>())
        };

        private static string GetKey(VMBrand b)
        {
            var first = b.Name.Substring(0, 1).ToLower();

            if (char.IsDigit(first[0]))
                return @"#";
            else
                return first;
        }

        protected override void Load(Action<VMBrand[]> results)
        {
            API.GetBrands(brands =>
            {
                results(brands.All.OrderBy(b => b.Name).Select(b => new VMBrand(b)).ToArray());
            }, error => results(null));
        }
    }

    public class VMBrand : VMEntries<ArticleEntry>
    {
        private Brand BackingBrand;
        public string Name { get { return BackingBrand.Name; } }

        public override string DisplayName { get { return @"Brand"; } }

        public VMBrand(Brand b)
        {
            BackingBrand = b;
        }

        protected override void LoadMoreImp(Action<APIResponse<ArticleEntry>> OnFinished, Action<Exception> OnError)
        {
            API.GetBrandEntries(LastPageLoaded + 1, BackingBrand.Id, OnFinished, OnError);
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
